# word 转 pdf
# pip install pywin32


from win32com.client import constants, gencache


def createPdf(wordPath, pdfPath):
    """
    word 转 pdf
    :param wordPath:    word文件路径
    :param pdfPath:     生成pdf文件路径
    """
    word = gencache.EnsureDispatch('Word.Application')
    doc = word.Documents.Open(wordPath, ReadOnly=1)
    doc.ExportAsFixedFormat(pdfPath,
                            constants.wdExportFormatPDF,
                            Item=constants.wdExportDocumentWithMarkup,
                            CreateBookmarks=constants.wdExportCreateHeadingBookmarks)
    word.Quit(constants.wdDoNotSaveChanges)


if __name__ == '__main__':
    # 这里路径是绝对路径，相对路径的话会报错
    createPdf('D:/WorkSpace/giteeSpace/PythonOfficeCases/1.office_cases/02.py_wordFile/03.违章通知书.docx',
              'D:/WorkSpace/giteeSpace/PythonOfficeCases/1.office_cases/02.py_wordFile/06.违章通知书.pdf')
